package com.health.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.health.dao.OrderSettingDao;
import com.health.service.OrderSettingService;
import com.healthcommon.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Transactional
@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /*
    * 批量添加文件内部读取的预约设置
    * */
    public void add(List<OrderSetting> orderSettingList) {
        /*
        * 循环添加，判断预约设置的日期是否已经存在，如果存在，则更新，如果不存在，则添加
        * */
        if(orderSettingList != null && orderSettingList.size() > 0){
            for (OrderSetting s:orderSettingList
                 ) {
//                先查询日期是否存在，如果存在进行更新，如果不存在，进行添加
                long count = orderSettingDao.findCountByOrderDate(s.getOrderDate());
                if(count>0){
//                    进行更新操作
                    orderSettingDao.editNumberByOrderDate(s);
                }else {
//                    进行添加操作
                    orderSettingDao.add(s);
                }
            }
        }

    }

    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin = date + "-1";
        String dateEnd = date + "-31";
        Map datemap=new HashMap<String,String>();
        datemap.put("dateBegin",dateBegin);
        datemap.put("dateEnd",dateEnd);
        List<OrderSetting> orderlist = orderSettingDao.getOrderSettingByMonth(datemap);
        List<Map> ordersettinglist =new ArrayList<Map>();
        for (OrderSetting s: orderlist
             ) {
            HashMap<String, Object> ordersettingmap = new HashMap<String, Object>();
            ordersettingmap.put("date",s.getOrderDate().getDate());//获得日期（几号）
            ordersettingmap.put("number",s.getNumber());//可预约人数
            ordersettingmap.put("reservations",s.getReservations());//已预约人数
            ordersettinglist.add(ordersettingmap);
        }
        return ordersettinglist;
    }

    public void editNumberByOrderDate(OrderSetting orderSetting) {
        /*
        * 根据日期修改预约人数，先查询是否存在，如果存在，则修改，如果不存在，则添加
        * */
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count > 0){
            //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingDao.editNumberByOrderDateitem(orderSetting);
        }else {
            //当前日期没有进行预约设置，进行添加操作
            orderSettingDao.add(orderSetting);
        }
    }

}
